package yxy.game.pm2.command.godBeast;

import yxy.cherry.data.bean.therionskill;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedGodBeastMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.godBeast.GodBeast;
import yxy.game.pm2.module.godBeast.PickedGodBeastHandle;
import yxy.game.pm2.module.godBeast.PickedGodBeastModule;

import java.util.Collection;

/**
 * @name CMD_PickedGodBeastSkillLevelUp
 * @author wrs
 * @descrition 神兽技能升级
 * @since 2022-05-31
 */
@MessageType(proto = Command.PickedGodBeastSkillLevelUp,classtype = PickedGodBeastMessage.PickedGodBeastSkillLevelUpRequest.class)
public class CMD_PickedGodBeastSkillLevelUp extends Command<PickedGodBeastMessage.PickedGodBeastSkillLevelUpRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastSkillLevelUpRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int code = request.getCode(); //神兽ID
        int skillLevel = request.getSkillLevel(); //技能等级

        PickedGodBeastMessage.PickedGodBeastSkillLevelUpResponse.Builder response = PickedGodBeastMessage.PickedGodBeastSkillLevelUpResponse.newBuilder();
        response.setCode(code);

        try {
            PickedGodBeastHandle handle = new PickedGodBeastHandle();

            PickedGodBeastModule pgbModule = PickedGodBeastModule.get(playerCode);
            if (pgbModule == null) {
                send(messageSource, response, Result.DataError);//不存在,参数错误
                return;
            }

            GodBeast godBeast = new PickedGodBeastHandle().getGodBeastById(pgbModule,code);
            if (godBeast == null) {
                send(messageSource, response, Result.DataError);//参数错误
                return;
            }

            boolean flag = handle.isNeedLevel(godBeast,skillLevel);
            if (!flag){
                send(messageSource,response, Result.GodBeastLevelLower); //神兽等级不够
                return;
            }

            if (godBeast.getSkillLevel() == handle.getGodBeastSkillMaxLevel(godBeast.getType())){
                send(messageSource,response, Result.GodBeastSkillLevelUpper); //技能等级达到上限
                return;
            }

            if (!handle.checkCost(playerCode,godBeast,skillLevel)){
                send(messageSource,response, Result.CostFail);//消费不足
            }
            response.setCode(godBeast.getCode()); //神兽ID


            therionskill therionskill = handle.getTherionSkill(godBeast, skillLevel); //下一级技能封装
            Collection<Currency> currencies = Currency.create(therionskill.getCosttype(), therionskill.getCostid(), therionskill.getCostnum());
            try {
                new CurrencyHandle().cost(playerCode, CCReason.神兽, currencies);
            } catch (CurrencyNotEnoughException e) {
                send(messageSource,response,new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));
                return;
            }

            godBeast.setSkillLevel(skillLevel + 1); //技能升级
            pgbModule.set(); //更新缓存

            for (GodBeast value : pgbModule.getGbs().values()) {
                response.addGbs(handle.getGodBeasts(playerCode,value));
            }

            send(messageSource,response, Result.Success);

        }catch (Exception e){
            e.printStackTrace();
            send(messageSource,response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastSkillLevelUpResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
